package ranab.server.ftp;

import java.io.File;
import java.net.Socket;
import java.net.InetAddress;

import ranab.util.ThreadPool;
import ranab.server.BaseServer;
import ranab.server.ServerEngine;
import java.util.*;

/**
 * This is ftp server starting point. For each request it
 * instantiates a new <code>BaseFtpConnection</code> object.
 * 
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public
class FtpServer extends BaseServer {                             
    
    // server name
    public final static String NAME = "FTP";
    
    // thread pool
    private ThreadPool mThreadPool;
    
    // configuration object
    private FtpConfig mConfig;
    
    //////////////////////////////////////////////
    /**
     * Constructor.
     * @param configFile this ftp server configuration file. 
     */
    public FtpServer(Properties configFile) throws Exception {
        mConfig = new FtpConfig(configFile);
            
        mThreadPool = new ThreadPool(mConfig.getThreadCount());
        mThreadPool.start();
    }

    
    //////////////////////////////////////////////
    /**
     * Constructor.
     * @param configFile this ftp server configuration file. 
     */
    public FtpServer(File configFile) throws Exception {
        mConfig = new FtpConfig(configFile);
        mThreadPool = new ThreadPool(mConfig.getThreadCount());
        mThreadPool.start();
    }
    
    /**
     * Get server port.
     */
    public int getServerPort() {
        return mConfig.getServerPort();
    }
     
    /**
     * Get server bind address.
     */
    public InetAddress getServerAddress() {
        return mConfig.getServerAddress();
    }
     
    /**
     * Get server name.
     */
    public String getServerName() {
        return NAME;
    }
     
    /**
     * Serves threaded request. 
     */
    public void serveRequest(Socket soc) {
        BaseFtpConnection ftpCon = null;
        if (System.getProperty("xbotts.home") == null) {
            ftpCon = new FtpConnection(mConfig, soc);
        }else {
            ftpCon = new BIEFtpConnection(mConfig, soc);
        }
            
        mThreadPool.add(ftpCon);
    }
    
    /**
     * Stop all currently executing threads.
     */
    public void dispose() {
        if (mThreadPool != null) {
            mThreadPool.dispose();
            mThreadPool = null;
        }
        if (mConfig != null) {
            mConfig.dispose();
            mConfig = null;
        }
    } 
    
    /**
     * Get the configuration object.
     */
    public FtpConfig getConfig() {
        return mConfig;
    }
     
    /**
     * Get max server connections - returns the thread count.
     */
    public int getMaxServerConnection() {
       return mConfig.getThreadCount();
    }
        
    public static ServerEngine startIT(Properties props) throws Exception{
        BaseServer server = new FtpServer(props);
        ServerEngine engine = new ServerEngine(server);
        engine.startServer();        
        return engine;
    }
    
    
    /////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////
    /**
     * Command line ftp server starting point.
     */
    public static void main(String args[]) {
        BaseServer server = null;
        try {
            server = new FtpServer(new File(args[0]));
            ServerEngine engine = new ServerEngine(server);
            engine.startServer();
        } catch (Exception ex) {
            ex.printStackTrace();
        }    
    }
}
